package com.wf.atm.client;


import com.wf.atm.pojo.*;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.IOException;

/**
 * @author Brainrain
 */
public class BusinessPanel extends JPanel {


    private Account accountA;
    private ATMClient atmClient;

    public BusinessPanel(ATMClient atmClient) {

        this.atmClient = atmClient;
        initComponents();
    }

    //点击返回按钮，跳转至登陆界面
    private void backButtonActionPerformed(ActionEvent e) {
        CardLayout cardLayout = (CardLayout) this.getParent().getLayout();
        cardLayout.show(this.getParent(), "loginPanel");

        //清空业务界面数据
        clearBusiness();
    }

    //点击提交按钮，执行业务操作，刷新当前页面
    //TODO 业务校验
    private void submitButtonActionPerformed(ActionEvent e) {

        long id = Long.parseLong(idLabelText.getText());

        double money = Double.parseDouble(moneyTextField.getText());

        String item = (String) comboBox.getSelectedItem();
        Account acc = null;
        String pwd;
        if (item != null) {
            switch (item) {
                case "存款":
                    try {
                        atmClient.printWriter.println("deposit," + id + "," + money);
                        atmClient.printWriter.flush();
                        Object account = atmClient.ois.readObject();
                        System.out.println(account);
                        if (account instanceof Account) {
                            acc = (Account) account;
                            JOptionPane.showMessageDialog(null, "存款成功！！");

                        } else {
                            JOptionPane.showMessageDialog(null, account);
                        }

                    } catch (IOException | ClassNotFoundException ex) {
                        System.out.println(ex.getMessage());

                    }
                    break;
                case "取款":
                    String password = JOptionPane.showInputDialog(null, "请输入您的密码:");
                    if (password != null) {
                        try {
                            atmClient.printWriter.println("withdraw," + id + "," + password + "," + money);
                            atmClient.printWriter.flush();

                            Object o = atmClient.ois.readObject();
                            if (o instanceof Account) {
                                acc = (Account) o;
                                JOptionPane.showMessageDialog(null, "取款成功！！");
                            } else {
                                JOptionPane.showMessageDialog(null, o);
                            }

                        } catch (IOException | ClassNotFoundException ex) {
                            throw new RuntimeException(ex);
                        }
                    }
                    //取款
                    break;
                case "转账":
                    pwd = JOptionPane.showInputDialog(null, "请输入您的密码:");
                    String to = JOptionPane.showInputDialog(null, "请输入对方账号:");
                    try {

                        atmClient.printWriter.println("transfer," + id + "," + pwd + "," + to + "," + money);
                        atmClient.printWriter.flush();
                        Object account = atmClient.ois.readObject();
                        System.out.println(account);
                        if (account instanceof Account) {
                            acc = (Account) account;
                            JOptionPane.showMessageDialog(null, "转账成功！！");

                        } else {
                            JOptionPane.showMessageDialog(null, account);
                        }

                    } catch (IOException | ClassNotFoundException ex) {
                        throw new RuntimeException(ex);
                    }
                    //转账
                    break;
                case "修改透支额度":
                    //查询余额
                    pwd = JOptionPane.showInputDialog(null, "请输入您的密码:");
                    try {

                        atmClient.printWriter.println("updateCeiling," + id + "," + pwd + "," + money);
                        atmClient.printWriter.flush();
                        Object account = atmClient.ois.readObject();
                        System.out.println(account);
                        if (account instanceof Account) {
                            acc = (Account) account;
                            JOptionPane.showMessageDialog(null, "修改透支额度成功！！");

                        } else {
                            JOptionPane.showMessageDialog(null, account);
                        }
                    } catch (IOException | ClassNotFoundException ex) {
                        throw new RuntimeException(ex.getMessage());
                    }
                    break;
                case "贷款":
                    //查询限额
                    try {

                        atmClient.printWriter.println("requestLoan," + id + "," + money);
                        atmClient.printWriter.flush();
                        Object account = atmClient.ois.readObject();
                        System.out.println(account);
                        if (account instanceof Account) {
                            acc = (Account) account;
                            JOptionPane.showMessageDialog(null, "贷款成功！！");

                        } else {
                            JOptionPane.showMessageDialog(null, account);
                        }
                    } catch (IOException |
                             ClassNotFoundException ex) {
                        throw new RuntimeException(ex);
                    }
                    break;
                case "还贷":
                    //还贷款
                    try {
                        atmClient.printWriter.println("payLoan," + id + "," + money);
                        atmClient.printWriter.flush();

                        Object account = atmClient.ois.readObject();
                        System.out.println(account);
                        if (account instanceof Account) {
                            acc = (Account) account;
                            JOptionPane.showMessageDialog(null, "还款成功！！");

                        } else {
                            JOptionPane.showMessageDialog(null, account);
                        }
                    } catch (IOException | ClassNotFoundException ex) {
                        throw new RuntimeException(ex);
                    }

                    break;
                default:
                    break;


            }
        }
        if (acc != null) {
            initBusiness(acc);
        } else {
            initBusiness(accountA);
        }

    }

    //清空业务界面数据
    public void clearBusiness() {
        idLabelText.setText("");
        nameLabelText.setText("");
        balanceLabelText.setText("");
        moneyTextField.setText(0.0 + "");
        ceilingLabelText.setText("");
        loanLabelText.setText("");
    }

    //初始化业务界面
    public void initBusiness(Account acc) {

        this.accountA = acc;
        idLabelText.setText(acc.getId() + "");
        nameLabelText.setText(acc.getName());
        balanceLabelText.setText(acc.getBalance() + "");
        moneyTextField.setText(0.0 + "");
        //判断账户类型 如果不是信用账户就 显示 "不具备透支功能" 如果是，则显示 透支额度金额 ceilingLabelText
        if (acc instanceof CreditAccount) {
            ceilingLabelText.setText(((CreditAccount) acc).getCeiling() + "");
        } else {
            ceilingLabelText.setText("不具备透支功能");
        }
        //判断账户类型 如果不是贷款账户就 显示 "不具备贷款功能" 如果是，则显示 贷款金额     loanLabelText
        if (acc instanceof LoanSavingAccount) {
            loanLabelText.setText(((LoanSavingAccount) acc).getLoanAmount() + "");
        } else if (acc instanceof LoanCreditAccount) {
            loanLabelText.setText(((LoanCreditAccount) acc).getLoanAmount() + "");
        } else {
            loanLabelText.setText("不具备贷款功能");
        }
        //根据不同的账户类型 确定 具有的业务功能
        //new DefaultComboBoxModel<>(new String[] {列表项的内容})
        DefaultComboBoxModel model = null;
        if (acc instanceof LoanSavingAccount) {//可贷款的储蓄账户
            model = new DefaultComboBoxModel<>(new String[]{"存款", "取款", "转账", "贷款", "还贷"});
        } else if (acc instanceof LoanCreditAccount) {//可贷款的信用账户
            model = new DefaultComboBoxModel<>(new String[]{"存款", "取款", "转账", "修改透支额度", "贷款", "还贷"});
        } else if (acc instanceof CreditAccount) {//信用账户
            model = new DefaultComboBoxModel<>(new String[]{"存款", "取款", "转账", "修改透支额度"});
        } else if (acc instanceof SavingAccount) {//储蓄账户
            model = new DefaultComboBoxModel<>(new String[]{"存款", "取款", "转账"});
        }
        comboBox.setModel(model);

    }


    /**
     * 初始化组件
     */
    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        title = new JLabel();
        idLabel = new JLabel();
        idLabelText = new JLabel();
        nameLabel = new JLabel();
        nameLabelText = new JLabel();
        balanceLabel = new JLabel();
        balanceLabelText = new JLabel();
        ceilingLabel = new JLabel();
        ceilingLabelText = new JLabel();
        loanLabel = new JLabel();
        loanLabelText = new JLabel();
        comboBox = new JComboBox<>();
        moneyTextField = new JTextField();
        submitButton = new JButton();
        backButton = new JButton();

        //======== this ========
        setLayout(null);

        //---- title ----
        title.setText("业 务 界 面");
        title.setFont(new Font("Microsoft YaHei", Font.PLAIN, 24));
        title.setHorizontalAlignment(SwingConstants.CENTER);
        add(title);
        title.setBounds(115, 55, 240, 50);

        //---- idLabel ----
        idLabel.setText("账      户");
        idLabel.setHorizontalAlignment(SwingConstants.CENTER);
        add(idLabel);
        idLabel.setBounds(90, 130, 90, 30);
        add(idLabelText);
        idLabelText.setBounds(185, 135, 190, 30);

        //---- nameLabel ----
        nameLabel.setText("姓      名");
        nameLabel.setHorizontalAlignment(SwingConstants.CENTER);
        add(nameLabel);
        nameLabel.setBounds(90, 190, 90, 30);
        add(nameLabelText);
        nameLabelText.setBounds(185, 195, 190, 30);

        //---- balanceLabel ----
        balanceLabel.setText("余      额");
        balanceLabel.setHorizontalAlignment(SwingConstants.CENTER);
        add(balanceLabel);
        balanceLabel.setBounds(90, 250, 90, 30);
        add(balanceLabelText);
        balanceLabelText.setBounds(185, 255, 190, 30);

        //---- ceilingLabel ----
        ceilingLabel.setText("信 用 额 度");
        ceilingLabel.setHorizontalAlignment(SwingConstants.CENTER);
        add(ceilingLabel);
        ceilingLabel.setBounds(90, 305, 90, 30);
        add(ceilingLabelText);
        ceilingLabelText.setBounds(185, 310, 190, 30);

        //---- loanLabel ----
        loanLabel.setText("贷 款 额");
        loanLabel.setHorizontalAlignment(SwingConstants.CENTER);
        add(loanLabel);
        loanLabel.setBounds(90, 360, 90, 30);
        add(loanLabelText);
        loanLabelText.setBounds(185, 360, 190, 30);

        //---- comboBox ----
        comboBox.setModel(new DefaultComboBoxModel<>(new String[]{
                "存款",
                "取款",
                "转账",
                "修改透支额度",
                "贷款",
                "还贷"
        }));
        comboBox.setPrototypeDisplayValue("存款,取款,转账,修改透支额度,贷款,还贷");
        add(comboBox);
        comboBox.setBounds(75, 410, 155, 40);
        add(moneyTextField);
        moneyTextField.setBounds(235, 410, 150, 40);

        //---- submitButton ----
        submitButton.setText("提交");
        submitButton.addActionListener(e -> submitButtonActionPerformed(e));
        add(submitButton);
        submitButton.setBounds(125, 485, 92, 30);

        //---- backButton ----
        backButton.setText("返回");
        backButton.addActionListener(e -> backButtonActionPerformed(e));
        add(backButton);
        backButton.setBounds(265, 485, 92, 30);

        setPreferredSize(new Dimension(500, 650));
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JLabel title;
    private JLabel idLabel;
    private JLabel idLabelText;
    private JLabel nameLabel;
    private JLabel nameLabelText;
    private JLabel balanceLabel;
    private JLabel balanceLabelText;
    private JLabel ceilingLabel;
    private JLabel ceilingLabelText;
    private JLabel loanLabel;
    private JLabel loanLabelText;
    private JComboBox<String> comboBox;
    private JTextField moneyTextField;
    private JButton submitButton;
    private JButton backButton;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
